
/* Generated by Interface Builder */

#import "Converter.h"
#import "Instrum.h"
#import <appkit/graphics.h>
#import "Statement.h"

@implementation Converter

+ initialize
{
   	ocimage = [NXImage findImageNamed:"octcps"];
   	opimage = [NXImage findImageNamed:"octpch"];
   	cpimage = [NXImage findImageNamed:"cpspch"];
   	coimage = [NXImage findImageNamed:"cpsoct"];
   	poimage = [NXImage findImageNamed:"pchoct"];
   	pcimage = [NXImage findImageNamed:"pchcps"];
	inOffset.x = 30;
	inOffset.y = 28;
	outOffset.x = 30;
	outOffset.y = 6;
	
	return self;
}

- init
{
	written = NO;
	size.width = 80.;
	size.height = 80.;
	center.x = size.width/2.;
	center.y = size.height/2.;
	strcpy(type,"Add");
	strcpy(name,"add");
	
	in = [[Param alloc] init:self :&inOffset];
	out = [[Param alloc] init:self :&outOffset];

	[in setTitle:"Input:"];

	paramList = [[List alloc] initCount:4];
	[paramList addObject:in];
	[paramList addObject:out];
	
	[Inst putUgenInList:self];
	
	return self;
}

- remove
{
	id cp,param;
	int i;
							// make sure not connected first 
	for(i = 0; i < ([paramList count]); i++) {	
		param = [paramList objectAt:i];
		if(cp = [param getConnectedParam]) 
			return 0;
	}
							// remove ugen from list
	[Inst removeUgenFromList:self];
	return self;
}

- (NXImage *)getImage
{						// get the right image
	return image;
}

- move:(NXPoint *)newloc
{
	location = *newloc;
	[in move:&location];
	[out move:&location];
	return self;
}

- setCtype:(int)n
{
	switch(n) {
		case 20: {
			image = ocimage;
			strcpy(name,"octcps");
			break;
		}
		case 21: {
			image = opimage;
			strcpy(name,"octpch");
			break;
		}
		case 22: {
			image = cpimage;
			strcpy(name,"cpspch");
			break;
		}
		case 23: {
			image = coimage;
			strcpy(name,"cpsoct");
			break;
		}
		case 24: {
			image = poimage;
			strcpy(name,"pchoct");
			break;
		}
		case 25: {
			image = pcimage;
			strcpy(name,"pchcps");
			break;
		}
	}
	strcpy(type,"Converter");
	return self;
}

- findParamAtPoint:(NXPoint *)point
{
	NXRect *rect;
	int i;
	id param;
	
	for(i = 0; i < [paramList count]; i++) {
		param = [paramList objectAt:i];
		rect = [param getRect];
		if(NXMouseInRect(point,rect,NO))
			return param;
	}
	//printf("no param found\n");
	return nil;
}

- writeUgen
{
	/* 	for each input param:
			see if there is input ugen
				if so, grab the output (ug?) and multiply it by this
					param value
				if not just use the param value
	 	after all params done, write relevant code into lists
			(declarations, assignments and loop statements)
			i.e. ug? = oscil(,,,,);
		go to output param connected ugen and call its "writeUgen"
	*/
	int i;
	int parent[3];
	id param;
	id ug;
	id nupar;
	char str[50];
	
	//printf("writing ugen %d\n",index);
	sprintf(str,"\tfloat ug%d;\n",index);
	[Inst putVarInList:str];
	sprintf(str,"\tfloat in%d;\n",index);
	[Inst putVarInList:str];


	for(i = 0; i < ([paramList count]-1); i++) {	
		param = [paramList objectAt:i];
		if(nupar = [param getConnectedParam]) {
			ug = [nupar getUgen];
			if([ug getWritten] == NO) { 
				parent[i] = [[ug writeUgen] getIndex];
			}
			else  {
				parent[i] = 0;
			}
			//printf("parent[%d] = %d\n",i,parent[i]);
		}
		else 
			parent[i] = 0;
		
	}
	
	sprintf(str,"\tin%d = %.2f;\n",index, 
		atof([[paramList objectAt:0] getValue]));
	[Inst putAssignInList:str];

	if(parent[0])
		sprintf(str,"\tug%d = %s(ug%d * in%d) ;\n", index, name, parent[0], index);
	else
		sprintf(str,"\tug%d = %s(in%d);\n", index, name, index, index);

	[Inst putAssignInList:str];
	written = YES;
	return self;	
}

@end
